package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcChanceEntity;
import com.sgb.jicai.view.activity.JcChanceListActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.HashMap;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class JcChanceListModel extends ViewModel {

    JcChanceListActivity activity;
    public void setActivity(JcChanceListActivity activitys) {
        this.activity = activitys;
    }
    private int page = 1;
    private int size = 10;
    public void getChance(int type, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        HashMap<String, Integer> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", size);
        if (type == 1) {
            map.put("opportunityType", 1);
            JiCaiBaseApi.getApi().offerChance(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<JcChanceEntity>>>>() {
                @Override
                public void onSuccess(BaseEntity<ListResponse<List<JcChanceEntity>>> result) {
                    if (result == null || result.getState() == null){
                        return;
                    }
                    activity.getListComplete();
                    if (result.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                        activity.setList(result.getData().getList(),isLoadMore );
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<JcChanceEntity>>>> call, Throwable t) {
                    super.onFailure(call, t);
                    activity.getListComplete();
                }
            });
        } else {
            map.put("opportunityType", 2);
            JiCaiBaseApi.getApi().joinChance(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<JcChanceEntity>>>>() {
                @Override
                public void onSuccess(BaseEntity<ListResponse<List<JcChanceEntity>>> result) {
                    if (result == null || result.getState() == null){
                        return;
                    }
                    activity.getListComplete();
                    if (result.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                        activity.setList(result.getData().getList(),isLoadMore );
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<ListResponse<List<JcChanceEntity>>>> call, Throwable t) {
                    super.onFailure(call, t);
                    activity.getListComplete();
                }
            });
        }

    }


    public void getH5Url(String id,String type) {
        String TYPE = "";
        if (type.equals("供方发起")){
            TYPE = "Factory";
        }else {
            TYPE = "collection";
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting(TYPE).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(JiCaiBaseApi.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(activity);
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LOGIN_TOKEN,"");
                    JiCaiBaseApi.startWeb(activity,url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
